//
//  SxwWebViewController.m
//  Pods-Runner
//
//  Created by Caijinglong on 2018/12/19.
//

#import "SxwWebViewController.h"
#import <WebKit/WebKit.h>
@interface SxwWebViewController ()<WKNavigationDelegate>

@end

@implementation SxwWebViewController

+ (instancetype)initWithUrl:(NSString *)url{
    SxwWebViewController *ctl = [[SxwWebViewController alloc]init];
    [ctl setUrl: url];
    return ctl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)];
    if([self navigationItem]){
        [[self navigationItem]setLeftBarButtonItem:closeItem];
    }
    [self.view addSubview: [self createWebView]];
}

-(UIView*) createWebView{
    WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, [self getWidth], [self getHeight])];
    
    NSURL *url = [[NSURL alloc]initWithString:self.url];
    if(url){
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        webView.navigationDelegate = self;
    }
    
    return webView;
}

-(void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(CGFloat) getWidth{
    return self.view.frame.size.width;
}

-(CGFloat) getHeight{
    return self.view.frame.size.height;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    if(self.navigationItem){
        [self.navigationItem setTitle:[webView title]];
    }
}

@end
